#include "log.h"

//%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%
//	Construction
//%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%
//========================================================================
//	Create Blank Log
//========================================================================
BGLLog::BGLLog() : m_engine(0), m_lines(0)
{
	m_pos[0] = 0;
	m_pos[1] = 0;
}
//========================================================================
//	Decon
//========================================================================
BGLLog::~BGLLog()
{
}
//========================================================================
//	Load Log
//========================================================================
void BGLLog::Load(WinGLengine* _engine, char* _font, int _cSize, int _lines, float x, float y)
{
	m_engine = _engine;
	m_font.Load(_engine,_cSize,_font);
	m_lines = _lines;
	m_pos[0] = x;
	m_pos[1] = y;
}
//%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%
//	Functions
//%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%
//========================================================================
//	Render flat
//========================================================================
void BGLLog::RenderFlat()
{
	//----------------------------
	//	Check Font is Ready
	//----------------------------
	if(m_font.m_id == 0)
		return;

	//----------------------------
	//	Display last X lines
	//----------------------------
	char* _buffer = new char[50*m_lines];
	memset(_buffer,0,sizeof(char)*(50*m_lines));
	int _size = int(m_data.size());

	for(int c = max(0,_size - m_lines); c < _size; ++c)
	{
		strcat(_buffer,m_data[c].m_msg);
		strcat(_buffer,"\n");
	}

	glColor3f(1.0f,1.0f,1.0f);
	m_font.RenderBlock(m_pos[0],m_pos[1],_buffer);
}
//========================================================================
// Load from file
//========================================================================
bool BGLLog::LoadFile(char* _filename)
{
	 return false;
}
//========================================================================
// Save to File
//========================================================================
bool BGLLog::SaveFile(char* _filename)
{
	return false;
}
//========================================================================
//	Add Message
//========================================================================
bool BGLLog::Print(int _flag, char* _message)
{
	int _len = int(strlen(_message));
	//------------------------
	//	Check Input
	//------------------------
	if((_len > 0) && (_len < 50) && (_flag > 0))
	{
		Lmsg _temp = {0};
		_temp.m_flag = _flag;
		strcpy(_temp.m_msg,_message);
		m_data.push_back(_temp);
		return true;
	}
	return false;
}
//========================================================================
//	Add Formated String
//========================================================================
bool BGLLog::PrintS(int _flag, const char* _format, ...)
{
	//---------------------------------
	//  Generate Text
	//---------------------------------
	if(_format == NULL)		
		return false;

	char _input[100] = {0};
	va_list _argPtr;

	va_start(_argPtr, _format);		
	vsprintf(_input, _format, _argPtr);	
	va_end(_argPtr);

	int _len = int(strlen(_input));
	//------------------------
	//	Check Input
	//------------------------
	if((_len > 0) && (_len < 50) && (_flag > 0))
	{
		Lmsg _temp = {0};
		_temp.m_flag = _flag;
		strcpy(_temp.m_msg,_input);
		m_data.push_back(_temp);
		return true;
	}
	return false;
}
//%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%
//	Set Functions
//%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%
//========================================================================
//	Build Font Display Lists
//========================================================================
void BGLLog::setFont(char* _font, int _size)
{
	m_font.Load(m_engine,_size,_font);
}
//========================================================================
//	Set Amount of lines to be printed
//========================================================================
void BGLLog::setLine(int _lines)
{
	m_lines = _lines;
}
//========================================================================
//	Set Position of window
//========================================================================
void BGLLog::setPos(float x, float y)
{
	m_pos[0] = x;
	m_pos[1] = y;
}
//%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%
//	Accesors
//%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%
//========================================================================
//	Get message
//========================================================================
const Lmsg* BGLLog::getLine(int _id) const
{
	if(_id == max(0,min(int(m_data.size()),_id)))
		return &(m_data[_id]);
	else
		return 0;
}
//========================================================================
//	Get Line Count
//========================================================================
int BGLLog::getLineCount() const
{
	return int(m_data.size());
}
//========================================================================
//	Return X
//========================================================================
float BGLLog::getX() const
{
	return m_pos[0];
}
//========================================================================
//	Return Y
//========================================================================
float BGLLog::getY() const
{
	return m_pos[1];
}